<template>
  <el-dialog
    :title="'降价处理'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <!-- <el-form-item label="spu_id" prop="spuId">
        <el-input v-model="dataForm.spuId" placeholder=""></el-input>
      </el-form-item> -->
      <el-form-item label="干货名称" prop="spuId">
        <el-input v-model="dataForm.spuName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="降价幅度" prop="reducedPrice">
        <el-input
          v-model="dataForm.reducedPrice"
          placeholder="降价幅度"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :plain="true" @click="open1">恢复原价</el-button>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      wareList: [],
      dataForm: {
        spuId: 0,
        spuName: "",
        reducedPrice: ""
      },
      dataRule: {
        spuId: [{ required: true, message: "spuId不能为空", trigger: "blur" }],
        reducedPrice: [
          { required: true, message: "降价幅度不能为空", trigger: "blur" }
        ]
      }
    };
  },

  mounted() {},

  methods: {
    open1() {
      this.$message({
        showClose: true,
        message: "如需恢复原价，请于降价幅度处输入0"
      });
    },
    init(id, spuName) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm.spuId = id;
        this.dataForm.spuName = spuName;
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          console.log(this.dataForm.spuId);
          this.$http({
            url: this.$http.adornUrl("/ware/oldtimers/update/reducedPrice"),
            method: "get",
            params: this.$http.adornParams({
              spuId: this.dataForm.spuId,
              reducedPrice: this.dataForm.reducedPrice
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error({
                message: data.msg,
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
